package com.taotao.decorate;

import com.alibaba.fastjson.JSONObject;
import com.taotao.entity.UserEntity;
import com.taotao.mapper.UserMapper;
import com.taotao.utils.JvmMapCacheUtils;
import com.taotao.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: wangjin
 * @CreateTime: 2022-06-22  21:14
 */
@Component
public class MySQLDecorate extends RedisDecorate  implements  AbstractDecorate{
    @Autowired
    private UserMapper userMapper;
    @Override
    public <T> T getCache(String key,Integer id) {
        UserEntity userEntity=super.getCache(key,id);
        if(userEntity!=null){
            return  (T)userEntity;
        }
        // 3.查询三级缓存 数据库
        UserEntity dbUser = userMapper.findByUser(id);
        if (dbUser == null) {
            return null;
        }
        // 三级缓存中数据存放在二级和一级缓存
        RedisUtil.setString(key, JSONObject.toJSONString(dbUser));
        JvmMapCacheUtils.putEntity(key, dbUser);
        // 扩展新增级别缓存 改动了
        return (T)dbUser;
    }
}
